<style>
    #class-add {
        padding: 20px 25px 25px 0;
    }
</style>
<div class="layui-fluid" id="class-add">
    <form class="layui-form layui-table-form" lay-filter="class-table-form">
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">批次：</label>
            <div class="layui-input-block">
                <select name="batchId" lay-filter="classAdd-cboBatch" id="classAdd-cboBatch" lay-search=""
                        lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">院校：</label>
            <div class="layui-input-block">
                <select name="collegeId" lay-verify="required" id="classAdd-cboCollege"
                        lay-filter="classAdd-cboCollege">
                    <option value=""></option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">层次：</label>
            <div class="layui-input-block">
                <select name="levelId" lay-verify="required" id="classAdd-cboLevel" lay-filter="classAdd-cboLevel">
                    <option value=""></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">专业：</label>
            <div class="layui-input-block">
                <select name="majorId" lay-verify="required" id="classAdd-cboMajor" lay-filter="classAdd-cboMajor">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">班主任：</label>
            <div class="layui-input-block">
                <input type="text" name="userId" id="classAdd-cboUser" lay-filter="classAdd-cboUser" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">唯一编码：</label>
            <div class="layui-input-block">
                <input type="text" name="className" disabled class="layui-input layui-disabled"
                       id="classAdd-className">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <textarea name="remark" lay-verify="txt" maxlength="100" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="class-add-form-submit" id="classAdd-submit"></button>
        </div>
    </form>
</div>
<script type="text/javascript" src="module/treeSelect/treeSelect.js"></script>
<script>
    layui.use(['febs', 'form','treeSelect', 'jquery', 'validate'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            treeSelect= layui.treeSelect,
            $view = $('#class-add'),
            validate = layui.validate;

        let major = '';
        let batch = '';
        let level = '';
        let college = '';

        form.verify(validate);
        form.render();

        //批次
        form.on('select(classAdd-cboBatch)', function (data) {
                batch = data.elem[data.elem.selectedIndex].text;
                $("#classAdd-className").val(batch + college + level + major);
        });

        //院校
        form.on('select(classAdd-cboCollege)', function (data) {
            if(data.value!==""){
                $.ajax({
                    url: ctx + 'college/findById/' + data.value,
                    success: function (res) {
                        college = res.data.simplename;
                        major = '';
                        level = '';
                        $("#classAdd-className").val(batch + college + level + major);
                    }
                })
            }else {
                 major = '';
                 level = '';
                 college = '';
                $("#classAdd-className").val(batch);
            }

            let collegeId = data.value;
            $("#classAdd-cboLevel").empty();
            $("#classAdd-cboLevel").append(new Option("", ''));
            $("#classAdd-cboMajor").empty();
            $("#classAdd-cboMajor").append(new Option("", ""));

            febs.get(ctx + 'college/levelSelect?collegeId=' + collegeId, null, function (data) {
                let list = data.data;
                for (let i = 0; i < list.length; i++) {
                    let level = list[i];
                    $("#classAdd-cboLevel").append(new Option(level.levelName, level.id));
                }
                form.render("select");
            });

        });

        //层次
        form.on('select(classAdd-cboLevel)', function (data) {
                level = data.elem[data.elem.selectedIndex].text;
            if(level!==''){
                $("#classAdd-className").val(batch + college + level + major)
            }else {
                major = '';
                level = '';
                $("#classAdd-className").val(batch + college)
            }

            let collegeId = $("#classAdd-cboCollege").val();
            let levelId = data.value;
            $("#classAdd-cboMajor").empty();
            $("#classAdd-cboMajor").append(new Option("", ""));
            febs.get(ctx + 'college/majorSelect?collegeId=' + collegeId + "&levelId=" + levelId, null, function (data) {
                let list = data.data;
                for (let i = 0; i < list.length; i++) {
                    let major = list[i];
                    $("#classAdd-cboMajor").append(new Option(major.fullName, major.id));
                }
                form.render("select");
            });

        });
        //专业
        form.on('select(classAdd-cboMajor)', function (data) {
            if(data.value!==""){
                $.ajax({
                    url: ctx + 'major/findById/' + data.value,
                    success: function (res) {
                        major = res.data.simpleName;
                        $("#classAdd-className").val(batch + college + level + major)
                    }
                })
            }else {
                major = '';
                $("#classAdd-className").val(batch + college + level)
            }

        });

        form.on('submit(class-add-form-submit)', function (data) {
            febs.post(ctx + 'class/add', data.field, function (data) {
                if(data.data){
                    layer.closeAll();
                    febs.alert.success('新增班级操作成功');
                    $('#businesses-class').find("#class-query").click();
                } else {
                    febs.alert.error('已有班级不能增加');
                }
            });
            return false;
        });

        //构建用户树
        treeSelect.render({
            // 选择器
            elem: '#classAdd-cboUser',
            // 数据
            data: ctx + 'class/userDeptTree',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '请选择',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            style: {
                folder: { // 父节点图标
                    enable: true // 是否开启：true/false
                },
                line: {
                    enable: true
                }
            },
            // 点击回调
            click: function(d){
                $("#classAdd-cboUser").val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
            }
        });

        //查询所有有效批次
        febs.get(ctx + 'college/batchSelect', null, function (data) {
            let list = data.data;
            for (let i = 0; i < list.length; i++) {
                let batch = list[i];
                $("#classAdd-cboBatch").append(new Option(batch.batchName, batch.id));
            }
            //重新渲染下拉菜单
            form.render("select");
        });

        //查询所有院校
        febs.get(ctx + 'college/collegeSelect', null, function (data) {
            let list = data.data;
            for (let i = 0; i < list.length; i++) {
                let college = list[i];
                $("#classAdd-cboCollege").append(new Option(college.name+'(简称：'+college.simplename+')', college.id));
            }
            //重新渲染下拉菜单
            form.render("select");
        });
    });
</script>